package com.tweetbook.resources;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import java.sql.Connection;

public class DBConnection {

	public Connection connectDB(){
		Connection conn = null;
		try {
			String user = "jenasdb";
            String password = "fastdb";
            String url = "jdbc:mysql://localhost/tweetbookdb";
            Class.forName ("com.mysql.jdbc.Driver").newInstance();
            conn = (Connection) DriverManager.getConnection(url, user, password);
		}
		catch(Exception e){
			System.out.println("Database connection threw "+e);
		}		
		
		return conn;
	}
	
	public Statement getStatement() throws SQLException{
		Connection conn = connectDB();
		Statement st = conn.createStatement();
		return st;
	}
	
	public void insertQry(String qry){
		try{
			getStatement().executeUpdate(qry);
		}
		catch(SQLException se){
			System.out.println("Exception occured during insertQry"+qry);
			se.printStackTrace();
		}
	}
	
	public ResultSet selectQry(String qry){
		ResultSet rs=null;
		try{
			rs = getStatement().executeQuery(qry);
		}
		catch(SQLException se){
			System.out.println("Exception occured during selectQry "+qry);
		}
		return rs;
		
	}
	
}
